package com.qy139.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qy139.auth.mapper.TabUserRoleMapper;
import com.qy139.auth.service.ITabUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qy139.entity.TabUserRole;
import com.qy139.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author yuhaochen
 * @since 2021-12-01
 */
@Service
public class TabUserRoleServiceImpl extends ServiceImpl<TabUserRoleMapper, TabUserRole> implements ITabUserRoleService {

    @Resource
    private TabUserRoleMapper userRoleMapper;
    @Override
    public Result getRolesIdByUid(Integer id) {
        return new Result(userRoleMapper.getRolesIdByUid(id));
    }

    @Override
    @Transactional
    public Result addRole(Integer uid, String checkedRoles) {
        QueryWrapper<TabUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        userRoleMapper.delete(queryWrapper);
        String[] roles = checkedRoles.split(",");
        for (String rid : roles) {
            TabUserRole userRole = new TabUserRole();
            userRole.setRid(Integer.parseInt(rid));
            userRole.setUid(uid);
            userRoleMapper.insert(userRole);
        }
        return new Result(200,"success","");
    }
}
